# -*- coding: utf-8 -*-
import requests
import json
from bs4 import BeautifulSoup
from app.db.database import SessionLocal
from app.db.crud import create_tpost, get_posts_count_with_title
from app.pacong.content_fetcher import ContentFetcher
import re

class BaiduHotListFetcher(ContentFetcher):
    def __init__(self):
        self.url = "https://top.baidu.com/board?tab=realtime"
        self.content_type = 2

    def my_fetch(self) -> str:
        """
        发送HTTP GET请求，获取指定URL的HTML内容。

        :return: 返回HTML内容。
        :raises: 如果请求失败，抛出HTTPError。
        """
        response = requests.get(self.url)
        response.raise_for_status()  # 检查请求是否成功
        return response.text

    def fetch_content(self):
        raw_data = self.my_fetch()

        # 使用正则表达式提取注释中的JSON字符串
        match = re.search(r"<!--s-data:(.*?)-->", raw_data, re.DOTALL)
        if not match:
            raise ValueError("No JSON data found in the HTML comment.")

        # 解析JSON字符串
        json_str = match.group(1)
        data = json.loads(json_str)

        # 过滤并转换数据
        result = []
        for card in data.get("data", {}).get("cards", []):
            for content in card.get("content", []):
                if not content.get("isTop"):
                    result.append({
                        "id": content["rawUrl"],
                        "title": content["word"],
                        "url": content["rawUrl"],
                        "extra": {
                            "hover": content.get("desc"),
                        }
                    })

        return result




if __name__ == "__main__":
    fetcher = BaiduHotListFetcher()
    fetcher.fetch_and_save()

    # 打印保存的内容
    # content = fetcher.fetch_content()
    # for item in content:
    #     print(json.dumps(item, ensure_ascii=False))